<?php

namespace model;

class loginModel {

	/**
	 * @var string
	 */
	private static $username = "Admin";

	private static $sessionUser = "USERNAME";
	private static $sessionPass = "PASSWORD";
	private static $sameSession = "SESSIONUSER";
	private static $userAgent = "HTTP_USER_AGENT";

	/**
	 * @var string
	 */
	private $password;

	/**
	 * the time when cookies expire
	 * @var int
	 */
	public $cookieDeath;

	/**
	 * @var boole
	 */
	public $userLoggedIn = false;

	/**
	 * @var boole
	 */
	public $userLoggingOut = false;

	/**
	 * @var boole
	 */
	public $cookieIsSet = false;

	/**
	 * initiate class member variables
	 */
	public function __construct() {
		$this->password = md5("Password");
		$this->cookieDeath = time() + 30;
	}

	/**
	 * @param  String $username
	 * @param  String $password
	 * @return void
	 */
	public function checkLogin($username, $password) {
		if (!$this->userLoggedIn && $this->cookieIsSet) {
			$this->validateCookie($username, $password);
		}
		else if (empty($username)) {
			throw new \Exception("Användarnamn Saknas");
		}
		else if (empty($password)) {
			throw new \Exception("Lösenord Saknas");	
		}
		else if ($username != self::$username || 
		  	$password != $this->password) {
			throw new \Exception("Felaktigt användarnamn och/eller lösenord");	
		}
		else {
			$this->doLogin();
		}
	}

	/**
	 * @return Boole
	 */
	public function checkSession() {
		if (isset($_SESSION[self::$sessionUser]) && isset($_SESSION[self::$sessionPass])) {
			return true;
		}		
	}

	/**
     * Check if session is valid or stolen
     * @return void
     */
    public function validateSession() {
    	if ($_SERVER[self::$userAgent] != $_SESSION[self::$sameSession]) {
			return true;
    	}
    }

	/**
	 * Set the username and password in the session
	 * @return void
	 */
	public function setSession() {
		$_SESSION[self::$sessionUser] = "Admin";
		$_SESSION[self::$sessionPass] = "Password";
		$_SESSION[self::$sameSession] = $_SERVER[self::$userAgent];
	}

   	/**
     * Check wether cookie is valid or not
     * @return void
     */
    private function validateCookie($username, $password) {    	
    	$cookieDeathTime = file_get_contents("cookieDeath.txt");
    	if ($username != self::$username || $password != $this->password) {
    		throw new \Exception("Felaktig information i cookie");
    	}
    	else if(time() > $cookieDeathTime) {
    		throw new \Exception("Felaktig information i cookie");
    	}
    	else {
    		$this->doLogin();
    	}
    }

    /**
     * determine which login method should be used and then login
     * @return void
     */
	public function doLogin() {
		if ($this->cookieIsSet && !$this->userLoggedIn) {
			$this->login();	
		}
		else if ($this->cookieIsSet) {
			file_put_contents("cookieDeath.txt", $this->cookieDeath);
			$this->login();
		}
		else if (isset($_SESSION[self::$sessionUser]) && isset($_SESSION[self::$sessionUser])) {
			$this->login();
		}		
		else {
			$this->login();
		}
	}

	/**
	 * set the login member variable to true
	 * @return void
	 */
	private function login() {
		$this->userLoggedIn = true;			
	}

	public function sessionUser() {
		if (isset($_SESSION[self::$sessionUser])) {
			return $_SESSION[self::$sessionUser];
		}
	}

	public function sessionPass() {
		if (isset($_SESSION[self::$sessionPass])) {
			return md5($_SESSION[self::$sessionPass]);
		}
	}

	/**
	 * Create logout message and set member variables to their correct values
	 * @return void
	 */
	public function logout() {
		$_SESSION = array();
    	session_destroy();
		$this->userLogingOut = true;
		$this->userLoggedIn = false;
	}
}